﻿using Engine.Graphics;
using Engine;
using Game;

namespace SAGhoul.ParticleSystem
{
	public class SoulsAttackParticleSystem : ParticleSystem<SoulsAttackParticleSystem.Particle>
	{
		public class Particle : Game.Particle
		{
			public Vector3 Velocity;
			public float Time;
			public float Duration;
		}

		public Game.Random m_random = new();
		public float m_time;
		public float m_toGenerate;
		public Vector3 m_start;
		public Vector3 m_end;
		public float m_scale;
		public SoulsAttackParticleSystem(Vector3 start, Vector3 end, float scale)
			: base(50)
		{
			Texture = ContentManager.Get<Texture2D>("Textures/FireParticle2");
			TextureSlotsCount = 3;
			m_start = start;
			m_end = end;
			m_scale = scale;
		}

		public override bool Simulate(float dt)
		{
			m_time += dt;
			float num = MathUtils.Lerp(150f, 20f, MathUtils.Saturate(2f * m_time / 0.5f));
			float num2 = MathUtils.Pow(0.01f, dt);
			float s = MathUtils.Lerp(20f, 0f, MathUtils.Saturate(2f * m_time / 0.5f));
			var v = new Vector3(2f, 2f, 1f);
			if (m_time < 0.5f)
			{
				m_toGenerate += num * dt;
			}
			else
			{
				m_toGenerate = 0f;
			}
			bool flag = false;
			for (int i = 0; i < Particles.Length; i++)
			{
				Particle particle = Particles[i];
				if (particle.IsActive)
				{
					flag = true;
					particle.Time += dt;
					if (particle.Time <= particle.Duration)
					{
						particle.Position += particle.Velocity * dt;
						particle.Velocity *= num2;
						particle.Velocity += v * dt;
						particle.TextureSlot = (int)MathUtils.Min(9f * particle.Time / particle.Duration, 8f);
						particle.Size = new Vector2(0.3f * m_scale);
					}
					else
					{
						particle.IsActive = false;
					}
				}
				else if (m_toGenerate >= 1f)
				{
					particle.IsActive = true;
					Vector3 v2 = m_random.Vector3(0f, m_scale);
					particle.Position = m_start + 0.3f * v2;
					particle.Color = Color.White;
					particle.Velocity = s * (m_end - m_start + m_random.Vector3(0f, 0.1f * m_scale)) + 2.5f * v2;
					particle.Size = Vector2.Zero;
					particle.Time = 0f;
					particle.Duration = m_random.Float(0.5f, 2f);
					particle.FlipX = m_random.Bool();
					particle.FlipY = m_random.Bool();
					m_toGenerate -= 1f;
				}
			}
			m_toGenerate = MathUtils.Remainder(m_toGenerate, 1f);
			if (!flag && m_time >= 0.5f)
			{
				return true;
			}
			return false;
		}
	}
}
